package com.langsheng.lsintell.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.langsheng.lsintell.R;
import com.langsheng.lsintell.config.LSLoginInfos;
import com.langsheng.lsintell.ui.activity.LSLoginActivity;
import com.langsheng.lsintell.ui.activity.LSRegisterActivity;
import com.langsheng.lsintell.utils.LSUtil;
import com.langsheng.lsintell.utils.LSConst;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LSMineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LSMineFragment extends LSBaseFragment {

    //    public final static String USER_DESC_GUEST = "0"; //访客
    public final static String USER_DESC_NONE = "欢迎 （普通用户） %s 登陆。\n购买解除试用码后升级为vip用户 "; //普通用户
    public final static String USER_DESC_AGENT = "欢迎 （经销商） %s 登陆。 "; //经销商
    public final static String USER_DESC_VIP = "欢迎 （VIP 用户） %1$s 登陆。 \n您的手机号绑定了 %2$s 个指纹所序列号。点击这里查看"; //VIP用户
    public final static String USER_DESC_ADMIN = "欢迎 （管理员） %1$s 登陆。 \n\n共有经销商数量：%2$s 人"; //管理员
    public final static String USER_DESC_FIANCEADMIN = "欢迎 （财务人员） %s 登陆。 "; //财务管理员
    public final static String USER_DESC_SUPERADMIN = "欢迎 （超级管理员） %1$s 登陆。 \n\n统计： \n普通用户数：%2$s  ， 管理员数: %3$s ，财务管理员数: %4$s ， \nVIP用户数: %5$s 。 总序列号数：%6$s 个"; //超级管理员

    @BindView(R.id.tv_login_btn)
    TextView tvLoginBtn;
    @BindView(R.id.tv_register_btn)
    TextView tvRegisterBtn;
    @BindView(R.id.ll_item_container1)
    LinearLayout llItemContainer1;
    @BindView(R.id.ll_item_container2)
    LinearLayout llItemContainer2;
    @BindView(R.id.ll_item_container3)
    LinearLayout llItemContainer3;
    @BindView(R.id.tv_sighed_desc)
    TextView tvSighedDesc;
    @BindView(R.id.ll_unLogin_view)
    LinearLayout llUnLoginView;

    public LSMineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment LSMineFragment.
     */
    public static LSMineFragment newInstance() {
        LSMineFragment fragment = new LSMineFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lsmine, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        initGuestItemList();
        initUnLoginView();
    }

    @Override
    public void onResume() {
        super.onResume();
        initMineItems();
    }

    private void initMineItems() {
        String ace = "-1";
        String user = "'";
        if (LSLoginInfos.getOurInstance().getLoginData() != null) {
            ace = LSLoginInfos.getOurInstance().getLoginData().getUserace();
            user = LSLoginInfos.getOurInstance().getLoginData().getMobile();
        }
        String info = "";
        boolean isShow = false;
        switch (ace) {
            case LSConst.USER_ACE_GUEST:
                initGuestItemList();
                break;
            case LSConst.USER_ACE_NONE:
                isShow = true;
                initNormalItemList();
                info = String.format(Locale.getDefault(), USER_DESC_NONE, user);
                break;
            case LSConst.USER_ACE_AGENT:
                isShow = true;
                initAgentItemList();
                info = String.format(Locale.getDefault(), USER_DESC_AGENT, user);
                break;
            case LSConst.USER_ACE_VIP:
                isShow = true;
                initVIPItemList();
                info = String.format(Locale.getDefault(), USER_DESC_VIP, user, "3");
                break;
            case LSConst.USER_ACE_ADMIN:
                isShow = true;
                initAdminItemList();
                info = String.format(Locale.getDefault(), USER_DESC_ADMIN, user, "6");
                break;
            case LSConst.USER_ACE_FIANCEADMIN:
                isShow = true;
                initFianceAdminItemList();
                info = String.format(Locale.getDefault(), USER_DESC_FIANCEADMIN, user);
                break;
            case LSConst.USER_ACE_SUPERADMIN:
                isShow = true;
                initSuperAdminItemList();
                info = String.format(Locale.getDefault(), USER_DESC_SUPERADMIN, user, "2", "3", "4", "5", "6");
                break;
            default:
                initGuestItemList();
                break;
        }
        showSignedView(info, isShow);
    }

    private void showSignedView(String desc, boolean isShow) {
        if (isShow) {
            tvSighedDesc.setText(desc);
            tvSighedDesc.setVisibility(View.VISIBLE);
            llUnLoginView.setVisibility(View.GONE);
        } else {
            tvSighedDesc.setVisibility(View.GONE);
            llUnLoginView.setVisibility(View.VISIBLE);
        }
    }


    private void initUnLoginView() {
        tvLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), LSLoginActivity.class);
                startActivity(i);

            }
        });

        tvRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), LSRegisterActivity.class);
                startActivity(i);
            }
        });
    }

    private View inflateItemView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.view_mine_item, null);
    }

    private View initItemView(int resIcon, String name, View.OnClickListener listener) {
        View view = inflateItemView();
        ImageView icon = (ImageView) view.findViewById(R.id.iv_item_icon);
        TextView itemName = (TextView) view.findViewById(R.id.tv_item_name);
        icon.setImageResource(resIcon);
        itemName.setText(name);
        view.setOnClickListener(listener);
        return view;
    }

    private View createDivideView() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LSUtil.dp2px(getActivity(), 1));
        ImageView view = new ImageView(getContext());
        view.setLayoutParams(params);
        view.setBackgroundColor(0x333333);
        return view;
    }

    /**
     * 访客界面
     */
    private void initGuestItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();

        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.GONE);
        llItemContainer3.setVisibility(View.GONE);

        addServerCenterView(llItemContainer1);
        llItemContainer1.addView(createDivideView());
        addAboutView(llItemContainer1);
    }

    /**
     * 普通用户
     */
    private void initNormalItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();
        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.GONE);
        llItemContainer3.setVisibility(View.VISIBLE);

        addBuyTrailCodeView(llItemContainer1);

        addmodifyPwdView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addServerCenterView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addLogoutView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addAboutView(llItemContainer3);
    }

    /**
     * 经销商
     */
    private void initAgentItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();
        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.GONE);
        llItemContainer3.setVisibility(View.VISIBLE);

        addStoreInfoView(llItemContainer1);

        addmodifyPwdView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addServerCenterView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addLogoutView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addAboutView(llItemContainer3);
    }

    /**
     * VIP用户
     */
    private void initVIPItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();
        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.GONE);
        llItemContainer3.setVisibility(View.VISIBLE);

        addBoughtSerialView(llItemContainer1);
        llItemContainer1.addView(createDivideView());
        addBuyTrailCodeView(llItemContainer1);

        addmodifyPwdView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addServerCenterView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addLogoutView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addAboutView(llItemContainer3);
    }

    /**
     * 普通管理员
     */
    private void initAdminItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();
        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.VISIBLE);
        llItemContainer3.setVisibility(View.VISIBLE);

        addSettingView(llItemContainer1);

        addAgencyMgrView(llItemContainer2);
        llItemContainer2.addView(createDivideView());
        addUserRegisterAddressView(llItemContainer2);
        llItemContainer2.addView(createDivideView());
        addShoppingMgrView(llItemContainer2);

        addmodifyPwdView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addServerCenterView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addLogoutView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addAboutView(llItemContainer3);
    }

    /**
     * 财务管理员
     */
    private void initFianceAdminItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();
        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.GONE);
        llItemContainer3.setVisibility(View.VISIBLE);

        addRemoveTrialView(llItemContainer1);

        addmodifyPwdView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addServerCenterView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addLogoutView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addAboutView(llItemContainer3);
    }

    /**
     * 超级管理员
     */
    private void initSuperAdminItemList() {
        llItemContainer1.removeAllViews();
        llItemContainer2.removeAllViews();
        llItemContainer3.removeAllViews();
        llItemContainer1.setVisibility(View.VISIBLE);
        llItemContainer2.setVisibility(View.VISIBLE);
        llItemContainer3.setVisibility(View.VISIBLE);

        addSettingView(llItemContainer1);

        addUserMgrView(llItemContainer2);
        llItemContainer2.addView(createDivideView());
        addUnbindSerialView(llItemContainer2);
        llItemContainer2.addView(createDivideView());
        addUserRegisterAddressView(llItemContainer2);
        llItemContainer2.addView(createDivideView());
        addShoppingMgrView(llItemContainer2);

        addmodifyPwdView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addServerCenterView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addLogoutView(llItemContainer3);
        llItemContainer3.addView(createDivideView());
        addAboutView(llItemContainer3);
    }

    /**
     * 服务中心
     *
     * @param container 父控件
     */
    private void addServerCenterView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_service_icon, getString(R.string.item_name_service_center), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO:打开客服界面
            }
        }));
    }

    /**
     * 关于
     *
     * @param container 父控件
     */
    private void addAboutView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_about, getString(R.string.item_name_about), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 设置
     *
     * @param container
     */
    private void addSettingView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_setting_icon, "设置", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 用户管理
     *
     * @param container
     */
    private void addUserMgrView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_user_mgr_icon, "用户管理", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 序列号解绑
     *
     * @param container
     */
    private void addUnbindSerialView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_serial_unbind, "序列号解绑", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 查看用户注册地
     *
     * @param container
     */
    private void addUserRegisterAddressView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_check_user, "查看用户注册地", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 商城管理
     *
     * @param container
     */
    private void addShoppingMgrView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_store_mgr, "商城管理", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 修改密码
     *
     * @param container
     */
    private void addmodifyPwdView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_modify_pwd, "修改密码", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 注销
     *
     * @param container
     */
    private void addLogoutView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_logout, "注销", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 设置解除试用金额
     *
     * @param container
     */
    private void addRemoveTrialView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_serial_unbind, "设置解除试用金额", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 经销商管理
     *
     * @param container
     */
    private void addAgencyMgrView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_check_user, "经销商管理", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 设置店铺信息
     *
     * @param container
     */
    private void addStoreInfoView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_store_mgr, "设置店铺信息", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 购买解除试用码
     *
     * @param container
     */
    private void addBuyTrailCodeView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_lock_serial_icon, "购买解除试用码", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    /**
     * 已购序列号
     *
     * @param container
     */
    private void addBoughtSerialView(LinearLayout container) {
        container.addView(initItemView(R.drawable.item_serial_icon, "已购序列号", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开关于
            }
        }));
    }

    @OnClick({R.id.tv_login_btn, R.id.tv_register_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_btn: {
                Intent intent = new Intent(getActivity(), LSLoginActivity.class);
                startActivity(intent);
            }
            break;
            case R.id.tv_register_btn: {
                Intent intent = new Intent(getActivity(), LSRegisterActivity.class);
                startActivity(intent);
            }
            break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
